;		MIDI_Test_v2.ASM            		       29/02/12
;**********************************************************************
;**********************************************************************
;                                                                     *
;    Filename:	    MIDI_Test_v2.ASM                                     *
;    Date:          29/02/12				                          *
;    File Version:  V 2.0                                             *
;                                                                     *
;    Author:        Jeffry Miller                                     *
;    Company:       Nova Scotia Community College - Kingstec          *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:  Let user know what other files are necessary    *
;                     p16F887.inc                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:  Testing sending note on and note off MIDI signals        *
;			 with the USART                                           *
;**********************************************************************


  LIST p=16f887		          	  ; list directive to define processor
  INCLUDE <p16f887.inc> 	      ; processor specific variable definitions

;**********************************************************************
		__CONFIG	 _CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF & _DEBUG_OFF
		
		__CONFIG	 _CONFIG2, _BOR40V & _WRT_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;********************************************************************** 
;********************** Header **********************************************
;**************** DEFINING VARIABLES ****************************************

		cblock 0x20				   ; General purpose registers
dataL
index	
endc

;************************ START OF PROGRAM **********************************
       org         0x0000          ; Address of the first program instruction
       goto        main            ; Go to label "main"
;************************ MAIN PROGRAM **************************************
main                               ; Start of the main program

       banksel     ANSEL           ; Bank containing register ANSEL
       clrf        ANSEL           ; Clears registers ANSEL and ANSELH
       clrf        ANSELH          ; All pins are digital
       
       banksel     TRISB           ; Selects bank containing register TRISB
	   movlw	   b'00000001'	   ; Set bit 0 to input, all others are outputs
	   movwf	   TRISB
       clrf		   TRISC		   ; Ensure TX pin is output

       banksel	   SPBRG
	   movlw	   d'07'		   ; Baudrate = (4000000)/(16*(07+1)) = 31 250
	   movwf	   SPBRG
	   bsf		   TXSTA,5		   ; Enable serial transmission 
	   bsf		   TXSTA,2		   ; Enable high baud rate
	   banksel	   RCSTA
	   bsf		   RCSTA,7		   ; Enable the serial port

	   call		   delay		   ; Wait until EUSART setup finishes

	   clrf		   index		   ; Point to first address in EEPROM

press_loop
	   banksel	   PORTB
	   btfsc	   PORTB,0		   ; Check for Button press
		   goto	   press_loop
	   call		   xmit
	   goto		   press_loop
;****************************************************************************************
; Subroutine to provide some settling time for start up
;****************************************************************************************	
delay
			banksel	dataL
			clrf	dataL

settle		decfsz	dataL,f
					goto settle			
			movf	RCREG,W
			movf	RCREG,W
			movf	RCREG,W
			return
;****************************************************************************************
; Transmit the Midi note A4 on a short delay then, A4 off via the USART
;****************************************************************************************
xmit
		   movlw	b'10010000'		; Note on, channel 0
		   call		transmit

		   movf		index,w			; Output the note pointed to by EEPROM index
		   banksel	EEADR
		   movwf	EEADR
		   banksel	EECON1
		   bsf		EECON1,RD		; Read from EEPROM
		   banksel	EEDAT
		   movf		EEDAT,w
		   banksel	PORTB

		   call		transmit
		   movlw	b'01111111'		; Medium velocity
		   call		transmit
		   banksel	   PORTB
release_loop   btfss   PORTB,0		; Check for Button press
		   	   goto	   release_loop
		   movlw	b'10000000'		; Channel mode message, channel 0
		   call		transmit

		   movf		index,w			; Output the note pointed to by EEPROM index
		   banksel	EEADR
		   movwf	EEADR
		   banksel	EECON1
		   bsf		EECON1,RD		; Read from EEPROM
		   banksel	EEDAT
		   movf		EEDAT,w
		   banksel	PORTB

		   call		transmit
		   movlw	b'01000000'		; Must be zero
		   call		transmit
		   call		delay
		   banksel PORTB
		   incf		index			; go to next note in scale
		   movlw	d'08'
		   subwf	index,w
		   btfsc	STATUS,Z		; Check if the index is out of bounds
				clrf	index		; Reset index
		   return			
;****************************************************************************************
; Transmit the character that is on WREG to MIDI if prior is sent
;****************************************************************************************
transmit
			banksel TXSTA
waittrn		btfss	TXSTA,TRMT
					goto waittrn
			banksel	TXREG
			movwf	TXREG

			return
;****************************************************************************************

       end                         ; End of program